const config = require("../db/config");
const externalApi = require("./externalApi");
const settingModel = require("../model/setting");
const proxyModel = require("../model/proxy");
const log4js      = require('../utils/logUtils');
const logger      = log4js.getLogger('service');

const TAG = "[拉流工具]";
module.exports = {
    /**
     * 把数据库的拉流信息和API里的拉流信息合并
     * @returns {Promise<*[]>}
     */
    async getAllPulls() {
        // 要确保 settingModel.cache有值
        const sipSetting = await settingModel.load();
        let pulls = [];
        if (config.proxyApi && config.proxyApi.host) {
            // 外部API接口,从api接口加载通道信息

            // 从api接口加载通道
            let token = await externalApi.login();
            pulls = await externalApi.getCameras(token);
            logger.debug(TAG, '从 API 接口获取通道信息', pulls);
        }

        // 数据库加载拉流通道
        const proxySource = await proxyModel.list(1, 10000,null);
        if (proxySource && proxySource.length > 0) {
            proxySource.map((item) => {
                item.recording=false;
                item.deviceid = sipSetting.sip_command_account;
                pulls.push(item);
            });
        }
        return pulls;
    }
}
